<template>
  <div>
    <div v-if="query.parentId === null">
      <div class="my-code">点击区域查看详情</div>
    </div>
    <div v-else>
      <!--工具栏-->
      <div class="head-container">
        <div v-if="crud.props.searchToggle">
          <!-- 搜索 -->
          <!-- 搜索 -->
          <el-input
            v-model="query.nameCn"
            clearable
            placeholder="区域名称"
            style="width: 185px;"
            class="filter-item"
            @keyup.enter.native="crud.toQuery"
          />
          <rrOperation :crud="crud" />
        </div>
        <crudOperation :permission="permission" />
      </div>
      <!--表单组件-->
      <el-dialog
        append-to-body
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="auto">
          <el-form-item label="中文区域名称" prop="nameCn">
            <el-input v-model="form.nameCn" />
          </el-form-item>
          <el-form-item label="英文区域名称" prop="nameEn">
            <el-input v-model="form.nameEn" />
          </el-form-item>
          <el-form-item label="是否可用" prop="status">
            <el-radio v-for="item in dict.true_false" :key="item.id" v-model="form.status" :label="item.dictKey">
              {{ item.dictValue }}
            </el-radio>
          </el-form-item>
          <el-form-item label="区域类型" prop="regionType">
            <el-radio v-for="item in dict.regionType" :key="item.id" v-model="form.regionType" :label="item.dictKey">
              {{ item.dictValue }}
            </el-radio>
          </el-form-item>
          <el-form-item label="配额" prop="deliveryLimit">
            <el-input-number v-model="form.deliveryLimit" :min="0" size="mini"       />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="nameCn" label="大厦"      >
          <template slot-scope="scope">
            <span>{{ scope.row.nameCn }}</span><br>
            <span>{{ scope.row.nameEn }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="是否可用"      >
          <template slot-scope="scope">
            {{ dict.label.true_false[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="regionType" label="区域类型"      >
          <template slot-scope="scope">
            {{ dict.label.regionType[scope.row.regionType] }}
          </template>
        </el-table-column>
        <el-table-column prop="deliveryLimit" label="配额"       />
        <el-table-column
          v-if="checkPer(['admin','orderRegion:edit','orderRegion:del'])"
          label="操作"
          width="150px"

        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import CRUD, { presenter, header, form } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import udOperation from '@crud/UD.operation'
import dictionaryData from '@/utils/dictionary'
import crudOrderRegion from '@/api/oms/orderRegion'
import crudOperation from '@crud/CRUD.operation'

const defaultForm = {
  id: null,
  nameCn: null,
  nameEn: null,
  status: false,
  regionType: 0,
  parentId: null,
  deliveryLimit: 0
}

export default {
  components: { pagination, rrOperation, udOperation, crudOperation },
  cruds() {
    return [
      CRUD({
        title: '字典详情', url: 'orderRegion/page', sort: 'id:desc', query: { parentId: null },
        crudMethod: { ...crudOrderRegion },
        queryOnPresenterCreated: false
      })
    ]
  },
  mixins: [
    presenter(),
    header(),
    form(function() {
      return Object.assign({ parentId: this.parentId }, defaultForm)
    })],
  dicts: ['true_false', 'regionType'],
  data() {
    return {
      parentId: null,
      allTag: dictionaryData.allTag,
      rules: {
        nameCn: [
          { required: true, message: '中文区域名称不能为空', trigger: 'blur' }
        ],
        nameEn: [
          { required: true, message: '英文区域名称不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '是否可用不能为空', trigger: 'blur' }
        ],
        regionType: [
          { required: true, message: '区域类型不能为空', trigger: 'blur' }
        ]
      },
      permission: {
        add: ['admin', 'orderRegion:add'],
        edit: ['admin', 'orderRegion:edit'],
        del: ['admin', 'orderRegion:del']
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeSubmit](crud) {
      crud.form.parentId = this.parentId
      return true
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
